<?php
$name = $_POST['name'];
$domain = $_POST['domain'];
$username = $_POST['username'];
$password = md5($_POST['password']);
$host = $_POST['host'];
$dbName = $_POST['dbname'];
$dbUser = $_POST['dbuser'];
$dbPass = $_POST['dbpass'];
// Write settings file
$file = "../3-includes/settings.php";
$fh = fopen($file, "w") or die("Couldn't create settings file");
fwrite($fh, "<"."?php\n");
fwrite($fh, "// Connection Information\n");
fwrite($fh, '$host = "'.$host.'";');
fwrite($fh, "\n");
fwrite($fh, '$DBUser = "'.$dbUser.'";'); fwrite($fh, "\n");
fwrite($fh, '$DBName = "'.$dbName.'";'); fwrite($fh, "\n");
fwrite($fh, '$DBPass = "'.$dbPass.'";'); fwrite($fh, "\n");
fwrite($fh, '$tableName = "3_text";'); fwrite($fh, "\n");
fwrite($fh, '$usersTable = "3_users";'); fwrite($fh, "\n");
fwrite($fh, '$settingsTable = "3_settings";'); fwrite($fh, "\n");
fwrite($fh, "// Don't change from here"); fwrite($fh, "\n"); 
fwrite($fh, '// Connect'); fwrite($fh, "\n");
fwrite($fh, '$'.'conn = @mysql_connect($'.'host, $'.'DBUser, $'.'DBPass) or die("Could not connect to database server");'); fwrite($fh, "\n"); 
fwrite($fh, '$'.'db = @mysql_select_db($'.'DBName) or die("Could not find database ".$'.'DBName." on server ".$'.'host.".");'); fwrite($fh, "\n");
fwrite($fh, '// Retrive Settings Array'); fwrite($fh, "\n");
fwrite($fh, '$'.'query = @mysql_query("SELECT * FROM `$'.'settingsTable` WHERE `id`=\'1\' LIMIT 1") or die("Query Error! Error Code: settings1");'); fwrite($fh, "\n");
fwrite($fh, '$'.'settings=@mysql_fetch_array($'.'query) or die("Array error! Error code: settings2");'); fwrite($fh, "\n"); 
fwrite($fh, '// Set Settings Variables!'); fwrite($fh, "\n");
fwrite($fh, "$"."version = $"."settings['version'];"); fwrite($fh, "\n");
fwrite($fh, "$"."isbeta = $"."settings['isbeta'];"); fwrite($fh, "\n");
fwrite($fh, "$"."betaMSG = $"."settings['betaMSG'];"); fwrite($fh, "\n");
fwrite($fh, "$"."domain = $"."settings['domain'];"); fwrite($fh, "\n");
fwrite($fh, "$"."theme = $"."settings['theme'];"); fwrite($fh, "\n");
fwrite($fh, '$'.'themeADR = $'.'domain."/3-content/themes/".$'.'theme."/".$'.'theme.".css";'); fwrite($fh, "\n");
fwrite($fh, "$"."name = $"."settings['name'];"); fwrite($fh, "\n");
fwrite($fh, "?>");
fclose($fh);
$file = "../.htaccess";
$fh = fopen($file, "w");
fwrite($fh, "RewriteEngine on"); fwrite($fh, "\n");
function replace_first($search, $replace, $data) {
    $res = strpos($data, $search);
    if($res === false) {
        return $data;
    } else {
        // There is data to be replaced
        $left_seg = substr($data, 0, strpos($data, $search));
        $right_seg = substr($data, (strpos($data, $search) + strlen($search)));
        return $left_seg . $replace . $right_seg;
    }
} 
$rb = replace_first("http://".$_SERVER['SERVER_NAME'], "", $domain);
$rb = replace_first(" ", "%20", $rb);
if ($rb == "") {
	fwrite($fh, "RewriteBase /");	
} else {
	fwrite($fh, "RewriteBase ".$rb);
}
fwrite($fh, "\n");
fwrite($fh, "RewriteCond %{REQUEST_FILENAME} !-f");
fwrite($fh, "\n");
fwrite($fh, "RewriteCond %{REQUEST_FILENAME} !-d");
fwrite($fh, "\n");
fwrite($fh, "RewriteRule ^/?([^/\.]+)/?$ show.php?id=$1 [L]");
fclose($fh);
$conn = mysql_connect($host, $dbUser, $dbPass);
$db = mysql_select_db($dbName);
mysql_query("CREATE TABLE IF NOT EXISTS `3_settings` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `version` text NOT NULL,
  `isbeta` text NOT NULL,
  `betaMSG` text NOT NULL,
  `domain` text NOT NULL,
  `theme` text NOT NULL,
  `name` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;");
mysql_query("CREATE TABLE IF NOT EXISTS `3_text` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `tid` text NOT NULL,
  `text` text NOT NULL,
  `ip` text NOT NULL,
  `timestamp` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;");
mysql_query("INSERT INTO `3_settings` (`id`, `version`, `isbeta`, `betaMSG`, `domain`, `theme`, `name`) VALUES
(1, '1.1', 'no', '', '$domain', 'white', '$name');");
mysql_query("CREATE TABLE IF NOT EXISTS `3_users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `username` text NOT NULL,
  `password` text NOT NULL,
  `name` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;");
mysql_query("INSERT INTO `3_users` (`id`, `username`, `password`, `name`) VALUES
(1, '$username', '$password', 'name');");
?> 